<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="common.jsp"%>
<%@page import="java.util.Collection,java.util.Iterator"%>
<jsp:useBean id="cart" scope="session" class="cn.jhc.bean.CartBean" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>吉他小店</title>
</head>
<body>
<jsp:include page="./header.jsp"></jsp:include>
	<%
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if (action != null && action.equals("保存修改")) {			
			String strItemNum = request.getParameter("itemnum");
			if (null == strItemNum || "".equals(strItemNum)) {
				throw new ServletException("非法的参数");
			}
			int itemNum = Integer.parseInt(strItemNum);
			for (int i = 0; i < itemNum; i++) {
				String strNum = request.getParameter("num_" + i);
				String strIsBn = request.getParameter("isbn_" + i);

				int quantity = Integer.parseInt(strNum);
				int isbn = Integer.parseInt(strIsBn);

				boolean bEnough = shopdb.isAmountEnough(isbn, quantity);
				if (bEnough) {
					cart.setItemNum(new Integer(isbn), quantity);
				} else {
					ShopBean shop = shopdb.getShop(strIsBn);
					out.println("<font color=\"red\" size=\"4\">");
					out.println(shop.getTitle());
					out.println("的库存只有" + shop.getAmount()
							+ "件，请调整购买数量！<p>");
					out.println("</font>");

				}

			}
		}
	%>
	<%
		
		Collection<CartItemBean>cl=cart.getItems();
		if(cl.size()<=0)
		{
			out.println("购物车中没有商品<p>");
		
	%>
		<a href="showallgoods.do?cond=all">继续购物</a>
	<%
		return;
		}
		Iterator<CartItemBean> it =cl.iterator();
	%>
		<form name="theform" action="showcart.jsp" method="post">
			<table BORDER="1">
				<tr>
					<th>名称</th>
					<th>价格</th>
					<th>数量</th>
					<th>小计</th>
					<th>取消</th>
				</tr>
		<%
			int i=0;
			while(it.hasNext()){
				CartItemBean cartItem=(CartItemBean)it.next();
				ShopBean shop=cartItem.getShop();
				String isbn=shop.getIsbn();
				String fieldNum="num_"+i;
				String fieldshop="isbn_"+i;
		%>		
			<tr>
				<td><%=shop.getTitle() %></td>
				<td><%=shop.getPrice() %></td>
				<td>
					<input type="text" name="<%=fieldNum %>"
					value="<%=cartItem.getQuantity() %>" aize="2"/>
					<input type="hidden" name="<%=fieldshop %>"
					value="<%=isbn %>"/>
				</td>
				<td><%=cartItem.getItemPrice() %></td>
				<td><a href="delitem.jsp?id=<%=isbn%>">删除</a></td>
				
			</tr>
		<%
			i++;
			}
		%>
			<tr>
			<td>合计</td>
			<td colspan="4"><%=cart.getTotalPrice() %></td>
			</tr>		
			</table><p>
			<input type="hidden" name="itemnum" value="<%=i %>"/>
			<input type="submit" name="action" value="保存修改"/>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<a href="showallgoods.do?cond=all">继续购物</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<a href="settlement.jsp?getb=/settlement.jsp">进入结算中心</a>
		</form>

</body>
</html>